package com.stool.gram.service.impl;

import com.stool.core.Classifier;
import com.stool.gram.domain.DLmodels;
import com.stool.gram.domain.GramRequest;
import com.stool.gram.domain.GramResult;
import com.stool.gram.domain.GramTime;
import com.stool.gram.service.DLService;
import com.stool.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DLServiceImpl implements DLService{

    @Autowired
    DLmodels dLmodels;

    @Override
    public GramResult computeResult(GramTime gramTime, String computeType) {
        Classifier classifier = dLmodels.get(computeType);
        float[][][] data = convertGramTime(gramTime);
        Result result = classifier.predict(data);
        GramResult gramResult = new GramResult();
        gramResult.setScore(result.getScore());
        gramResult.setLabel(result.getLabel());
        return gramResult;
    }

    @Override
    public List<GramResult> computeResults(List<GramTime> gramTimes, String computeType) {
        List<GramResult> gramResults = new ArrayList<>();
        for (GramTime gramTime : gramTimes) {
            GramResult gramResult = computeResult(gramTime, computeType);
            gramResults.add(gramResult);
        }
        return gramResults;
    }

    private float[][][] convertGramTime(GramTime gramTime) {
        float[][][] data = { { {gramTime.getTime0()}, {gramTime.getTimeNext()} } };
        return data;
    }

}
